package oa.hleast.xswl.controller.app.activity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.enums.QueryModeEnum;
import oa.hleast.common.result.Result;
import oa.hleast.common.result.ResultCode;
import oa.hleast.common.web.util.RequestUtils;
import oa.hleast.xswl.common.annotation.UserAuth;
import oa.hleast.xswl.common.enums.ActivityUserTypeEnum;
import oa.hleast.xswl.pojo.domain.UmsMember;
import oa.hleast.xswl.pojo.domain.activity.CmsActivityCalendar;
import oa.hleast.xswl.pojo.domain.activity.CmsActivityTemplate;
import oa.hleast.xswl.pojo.form.activity.ActivitySignUpForm;
import oa.hleast.xswl.pojo.vo.activity.ShowActivityCalendarVo;
import oa.hleast.xswl.pojo.vo.activity.ShowCommonActivityVo;
import oa.hleast.xswl.pojo.vo.activity.TodayCommonActivityVo;
import oa.hleast.xswl.service.IUmsAddressService;
import oa.hleast.xswl.service.IUmsUserService;
import oa.hleast.xswl.service.activity.*;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "【移动端】 通用活动报名")
@RestController
@RequestMapping("/api.app/v1/activity/commend")
@Slf4j
@AllArgsConstructor
public class ActivityCommendController {

    private final ICmsCommonActivityService commonActivityService;
    private final ICmsActivityCalendarService calendarService;
    private final ICmsActivityTemplateService templateService;
    private final ICmsUserSignUpService signUpService;
    private final ICmsUserSignInService signInService;
    private final IUmsUserService memberFeignService;

    //活动列表展示
    @ApiOperation(value = "活动列表分页", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryMode", value = "查询模式", paramType = "query", dataType = "QueryModeEnum",dataTypeClass = QueryModeEnum.class),
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页数量", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "activityTypeId", value = "活动类型ID", paramType = "query", dataType = "Long",dataTypeClass = Long.class),
    })
    @GetMapping("/pageList")
    public Result list(@RequestParam @NotNull String queryMode, @RequestParam @NotNull Integer page, @RequestParam @NotNull Integer limit, Long activityTypeId) {
        QueryModeEnum queryModeEnum = QueryModeEnum.getValue(queryMode);
        switch (queryModeEnum) {
            default:
                Page<ShowCommonActivityVo> result = commonActivityService.findShowActivity(page, limit, activityTypeId);
                return Result.success(result.getRecords(), result.getTotal());
        }
    }

    // 活动详情
    @ApiOperation(value = "活动详情", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "活动id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable @NotNull Long id) {
        Long userId = RequestUtils.getUserId();
        ShowCommonActivityVo vo = commonActivityService.detail(id, userId);
        return Result.success(vo);
    }

    /**
     * 活动某月的日期
     */
    @ApiOperation(value = "活动某月的日期", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "month", value = "月", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "activityId", value = "活动id", required = true, paramType = "query", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/findCalendarAfterToday")
    public Result findActivityAfterToday(@Valid @RequestParam @NotNull Long activityId,
                                         @Valid @RequestParam @NotNull Integer year,
                                         @Valid @RequestParam @NotNull Integer month) {
        List<CmsActivityCalendar> voList = commonActivityService.findActivityByTime(activityId, year, month);
        return Result.success(voList);
    }

    @ApiOperation(value = "活动能报名的日期(从今天起包含今天)", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class),
            @ApiImplicitParam(name = "activityId", value = "活动id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    })
    @GetMapping("/pageCalendarAfterToday")
    public Result pageActivityAfterToday(@Valid @RequestParam @NotNull Long activityId, @RequestParam @NotNull Integer page, @RequestParam @NotNull Integer limit) {
        List<ShowActivityCalendarVo> voList = commonActivityService.pageActivityAfterToday(page, limit, activityId);
        return Result.success(voList);
    }

    // 活动报名表
    @ApiOperation(value = "活动报名并获取报名表", httpMethod = "GET")
    @ApiImplicitParam(name = "id", value = "活动报名类型id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/template/detail")
    public Result templateDetail(@Valid @RequestParam @NotNull Long activityId) {
        CmsActivityTemplate vo = commonActivityService.signInAndGetTemplate(activityId);
        return Result.success(vo);
    }

    // 活动报名
    @UserAuth
    @ApiOperation(value = "确认报名", httpMethod = "POST")
    @ApiImplicitParam(name = "form", value = "活动报名", required = true, paramType = "body", dataType = "ActivitySignUpForm",dataTypeClass = ActivitySignUpForm.class)
    @PostMapping("/signUp")
    public Result signUp(@Valid @RequestBody ActivitySignUpForm form) {
        Long userId = RequestUtils.getUserId();
        log.info("userId = {}", userId);

        UmsMember user = memberFeignService.getById(userId);
        if (user == null) {
            return Result.failed(ResultCode.USER_NOT_EXIST);
        }
        Integer authStatus = user.getAuthStatus();
        if (0 == authStatus) {
            return Result.failed(ResultCode.USER_NOT_PHONE);
        } else if (1 == authStatus) {
            return Result.failed(ResultCode.USER_VERIFIED);
        }

        Boolean result = signUpService.userSignUp(userId, form);
        return Result.judge(result);
    }

    /**
     * 用户报名的培训预约
     */
    @UserAuth
    @ApiOperation(value = "用户报名的培训预约 -废弃", httpMethod = "GET")
    @GetMapping("/findUserActivity")
    public Result findUserActivity() {
        Long userId = RequestUtils.getUserId();

        List<TodayCommonActivityVo> list = signUpService.findUserActivity(userId);
        return Result.success(list);
    }

    /**
     * 用户报名的培训预约类/普通类活动
     */
    @UserAuth
    @ApiOperation(value = "用户报名的所有活动", httpMethod = "GET")
    @GetMapping("/findAllUserActivity")
    public Result findAllUserActivity() {
        Long userId = RequestUtils.getUserId();


        List<TodayCommonActivityVo> list = signUpService.findAllUserActivity(userId);
        return Result.success(list);
    }

    /**
     * 用户的场馆预约
     */
    @ApiOperation(value = "用户的场馆预约 - 费弃", httpMethod = "GET")
    @GetMapping("/findUserVenue")
    public Result findUserVenue() {
        Long userId = RequestUtils.getUserId();


        List<TodayCommonActivityVo> list = signUpService.findUserVenue(userId);
        return Result.success(list);
    }


    /**
     * 根据类型code获取用户报名的所有活动
     */
    @UserAuth
    @ApiOperation(value = "根据类型code获取用户报名的所有活动", httpMethod = "GET")
    @GetMapping("/findUserActivityByCode")
    public Result findUserActivityByCode(@RequestParam @NotNull String code) {
        Long userId = RequestUtils.getUserId();

        List<TodayCommonActivityVo> list = signUpService.findUserActivityByCode(userId, code);
        return Result.success(list);
    }


    // 用户签到
    @UserAuth
    @ApiOperation(value = "用户签到", httpMethod = "GET")
    @GetMapping("/signIn")
    public Result signIn(@RequestParam @NotNull Long activityId, @RequestParam @NotNull Long calenderId) {
        Long userId = RequestUtils.getUserId();

        Boolean result = commonActivityService.userSignIn(userId, activityId, calenderId, ActivityUserTypeEnum.STUDENT.getCode());
        return Result.judge(result);
    }


    @ApiOperation(value = "根据活动ID获取报名模板", httpMethod = "GET")
    @ApiImplicitParam(name = "activityId", value = "活动id", required = true, paramType = "path", dataType = "Long",dataTypeClass = Long.class)
    @GetMapping("/getTemplateByActivityId")
    public Result getTemplateByActivityId(@RequestParam @NotNull Long activityId) {
        CmsActivityTemplate vo = templateService.getTemplateByActivityId(activityId);
        return Result.success(vo);
    }

    //取消活动报名




}
